package cn.edu.zjgsu.ito.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.ParseException;
import java.util.List;

import cn.edu.zjgsu.ito.model.Major;
import cn.edu.zjgsu.ito.model.Payment;
import cn.edu.zjgsu.ito.service.AdminPaymentManageService;
import cn.edu.zjgsu.ito.service.ClassService;
import cn.edu.zjgsu.ito.service.EduSeasonService;
import cn.edu.zjgsu.ito.service.EduStartDateService;
import cn.edu.zjgsu.ito.service.MajorNameService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

public class AdminPayManageController extends Controller {
	AdminPaymentManageService adminPaymentManageService=ServiceFactory.createAdminPaymentManageService();
	private MajorNameService majorService = ServiceFactory.createMajorNameService();
	private EduStartDateService eduStartDateService = ServiceFactory.createEduStartDateService();
	private EduSeasonService eduSeasonService = ServiceFactory.createEduSeasonService();
	private ClassService classService = ServiceFactory.createClassService();
	private List<Record> Record1;
	private List<Record> Record2;
	private List<Record> Record3;
	private List<Record> Record4;
	private List<Record> Records;
	private String majorName;
	private String eduStartDate;
	private String eduSeason;
	private String classnumber;
	private String clazz;
	/**
	 * 页面初始化
	 * @throws ParseException 
	 */
	public void index() throws ParseException
	{
		Record1=majorService.allMajorName();	//获取所有专业
		setAttr("allMajorName",Record1);
		Record2 = eduStartDateService.allEduStartDate();//获取入学年份
		setAttr("allEduStartDate",Record2);
		Record3 = eduSeasonService.allEduSeason();//获取入学季度
		setAttr("allEduSeason",Record3);
		Record4=classService.allClass();//获取所有班级
		setAttr("allclass",Record4);
		System.out.println("121212121212121212121");
		List<Record> payment = adminPaymentManageService.paymentManage();
		setAttr("payment",payment);
		//获取所有学生信息
		List<Record> stuInfo = adminPaymentManageService.stuInfo();
		setAttr("stuInfo",stuInfo);
		render("paymanage.html");
	}
	/**
	 * 新建缴费
	 * 刘阳
	 */
	public void addpayment(){
		String passportNoes = getPara("passportNo");
		String payMethod = getPara("payMethod");
		String amount1 = getPara("amount");
		String paymentData = getPara("paymentData");
		String invoiceDate = getPara("invoiceDate");
		String remarks = getPara("remarks");
		float amount = Float.parseFloat(amount1);
		adminPaymentManageService.addPayment(passportNoes,payMethod,amount,paymentData,invoiceDate,remarks);
	}
	/**
	 * 刘阳
	 * 点击查看以往缴费记录
	 * @throws ParseException 
	 */
	public void ajaxlook() throws ParseException{
		String passportNo = getPara("passportNo");
		Record payment = new Record();
		payment.set("passportNo", passportNo);
		List<Record> information =  adminPaymentManageService.looks(payment);
		System.out.println(information);
		String temp = null;
		temp = JsonKit.toJson(information);		
		renderJson(temp);
	}
	/**
	 * 初始化缴费周期
	 * 刘阳
	 */
	public void ajaxindex(){
		String passportNo = getPara("passportNo");
		Record eduStartDate = Db.findFirst("select eduStartDate from student_info where passportNo=?",passportNo);
		String eduStartDates= eduStartDate.getStr("eduStartDate");
		List<String> error = adminPaymentManageService.paymentdataManage(eduStartDates);
		for(int i = 0;i < error.size();i++){
			System.out.println(error.get(i));
		}
		renderJson(error);
	}
	/**
	 * 条件搜索
	 * 刘阳
	 * @throws ParseException 
	 */
	public void newsearch() throws ParseException{
		String majorName = getPara("majorName");
		String clazz = getPara("clazz");
		if((majorName==null||majorName.equals(""))&&( clazz==null||clazz.equals(""))){
			
			redirect("/toErrors");
		}
		List<Record> news= adminPaymentManageService.selectNew(majorName,clazz);
		System.out.println(news);
		String temp = null;
		temp = JsonKit.toJson(news);
		renderJson(temp);
		
	}
	/**
	 * 刘阳
	 * 添加缴费记录
	 */
	public void ajaxupdate(){
//		String temp1 = "a,b,c,d";
//		String [] b = temp1.split(",");
//		for(int i=0;i<b.length;i++){
//			System.out.println(b[i]);
//		}
//		List<Record> paymentdata = adminPaymentManageService.paymentdata();
		String payMethod = getPara("payMethod");
		String amount = getPara("amount");
		String paymentData = getPara("paymentData");
		String invoiceDate = getPara("invoiceDate");
		String remarks = getPara("remarks");
		String passportNo = getPara("passportNo");//		int id=getParaToInt("id");
		Record payment = new Record();
		Record firstName1 = Db.findFirst("select firstName from student_info where passportNo=?",passportNo);
		Record lastName1 =  Db.findFirst("select lastName from student_info where passportNo=?",passportNo);
		Record sNo1 = Db.findFirst("select sNo from student_info where passportNo=?",passportNo);
		String firstName = firstName1.getStr("firstName");
		String lastName = lastName1.getStr("lastName");
		String sNo = sNo1.getStr("sNo");
		System.out.println(firstName1.getStr("firstName"));
		System.out.println("^^^^^^^^^^^^^^^^^^^^");
		payment.set("payMethod", payMethod);
		payment.set("amount", amount);
		payment.set("paymentData", paymentData);
		payment.set("invoiceDate", invoiceDate);
		payment.set("remarks", remarks);
		payment.set("passportNo", passportNo);
		payment.set("firstName", firstName);
		payment.set("lastName", lastName);
		payment.set("sNo", sNo);
		System.out.println("22222222222222222222222");
		String error = adminPaymentManageService.updatePayment(payment);
		System.out.println("1111111111111112222222222222223333333333333334444444444444");
		Record information = adminPaymentManageService.look(payment);
//		setAttr("payment",Major.dao.find("select * from major where id=?",id));
		System.out.println("66666666666666666666");
		setAttr("error",error);
		System.out.println(error);
		System.out.println(payment);
		renderJson();
		String temp = null;
		temp = JsonKit.toJson(information);		
		renderJson(temp);
	}
	//模板上传
	public void fileUpload(){
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));
			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				FileOutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				fos.close(); 
				fis.close();
				source.delete();
				String error=adminPaymentManageService.readPaymentTemplate(fileName, newFileName, filedir);
				setAttr("error",error);
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	//模板下载
	public void fileDownload()
	{		
		String fileName="缴费模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;	
		renderFile(new File(path));		
	}
}
